Little changes on Colombian Programming Contest solutions.
[and.git] / 439 - Knight moves / 439.cpp
blob55726b3e61b02c10ae40b88f9b4868c8ec097ed6
1 #include <iostream>
2 #include <set>
3 #include <queue>
5 using namespace std;
7 typedef pair<int, int> point;
9 int dj[] = {-2, -1, +1, +2, +2, +1, -1, -2};
10 int di[] = {-1, -2, -2, -1, +1, +2, +2, +1};
12 int main(){
13 string s, t;
14 while (cin >> s >> t){
15 point start(s[0]-'a', s[1]-'1');
16 point end(t[0]-'a', t[1]-'1');
18 set<point> visited;
19 queue<pair<point, int> > q;
20 q.push(make_pair(start, 0));
22 while (!q.empty()){
23 int dist = q.front().second;
24 point u = q.front().first;
25 q.pop();
26 if (0 <= u.first && u.first < 8 &&
27 0 <= u.second && u.second < 8 && !visited.count(u)){
28 //cout << "Visiting " << u.first << " " << u.second << endl;
29 if (u == end){
30 cout << "To get from "<<s<<" to "<<t<<" takes "<<dist<<" knight moves." << endl;
31 break;
33 visited.insert(u);
34 for (int k=0; k<8; ++k){
35 q.push(make_pair(point(u.first + di[k], u.second + dj[k] ), dist + 1));
40 return 0;